﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace CoolCode.Web.UI.Views.Home
{
    public partial class RecoverPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnlChangePassword.Visible = IsPostBack;
            ChangePassword1.Visible = IsPostBack;
            ChangePassword1.ContinueDestinationPageUrl = "~";
            ChangePassword1.ChangePasswordFailureText = "密码错误或新密码无效。新密码的最短长度为6";
            this.SubmitButton.Click += new EventHandler(SubmitButton_Click);
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            ChangePassword1.Visible = false;
            pnlChangePassword.Visible = false;
            var user = Membership.GetUser(this.UserName.Text);
            if (user != null && user.Email.Equals(this.Email.Text))
            {
                string newPassword = Membership.Provider.ResetPassword(user.UserName, "Yes, I like!");
                if (!User.Identity.IsAuthenticated)
                    FormsAuthentication.SetAuthCookie(user.UserName, false);
                FailureText.Text = string.Format("你的新密码是：<b>{0}</b><br/>请通过下面的录入来修改你的密码。", newPassword);
                ChangePassword1.Visible = true;
                pnlChangePassword.Visible = true;
               // ChangePassword1.CurrentPassword = newPassword;
            }
            else
            {
                if (user == null)
                {
                    FailureText.Text = "用户名不存在！";
                }
                else
                {
                    FailureText.Text = "Email不正确！";
                }
            }
        }
    }
}
